package com.zxl.live.screen.call.ui.widget;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zxl.live.screen.R;
import com.zxl.live.screen.call.helper.ThemeHelper;
import com.zxl.live.screen.call.model.bean.ThemeBean;
import com.zxl.live.screen.call.ui.activity.CallThemeDetailActivity;
import com.zxl.live.screen.ui.widget.GrildRecyclerDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/8.
 */

public class CallRecycleView extends RecyclerView {

    private List<ThemeBean> mList;

    public CallRecycleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setHasFixedSize(true);
        setLayoutManager(new GridLayoutManager(context, 2));
        setAdapter(new CallAdapter());
        addItemDecoration(new GrildRecyclerDecoration(AutoRoundLayout.getSpace(), 2, false));
    }

    public void setList(List<ThemeBean> mList) {
        this.mList = mList;
        getAdapter().notifyDataSetChanged();
    }

    public void refreshData() {
        if (null != mList && !mList.isEmpty()) {
            getAdapter().notifyDataSetChanged();
        }
    }

    private class CallAdapter extends RecyclerView.Adapter<CallViewHolder> {

        @Override
        public CallViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new CallViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.adapter_item_screen_led, parent, false));
        }

        @Override
        public void onBindViewHolder(CallViewHolder holder, int position) {
            holder.bind(mList.get(position), position);
        }

        @Override
        public int getItemCount() {
            return mList == null ? 0 : mList.size();
        }
    }

    class CallViewHolder extends ViewHolder implements OnClickListener {

        private int position;

        private View mCurrentView;

        private InCallActionView mInCallActionView;

        private ThemePreviewWindow mThemePreviewWindow;

        public CallViewHolder(View itemView) {
            super(itemView);
            mThemePreviewWindow = (ThemePreviewWindow) itemView.findViewById(R.id.preview_window);
            mInCallActionView = (InCallActionView) itemView.findViewById(R.id.call_action);
            mCurrentView = itemView.findViewById(R.id.item_selected_cover);
            itemView.findViewById(R.id.click_enter).setOnClickListener(this);
        }

        public void bind(ThemeBean themeBean, int position) {
            this.position = position;
            mThemePreviewWindow.refreshTheme(themeBean, AutoRoundLayout.getCallWidth(), AutoRoundLayout.getCellHeight());
            mInCallActionView.refreshTheme(themeBean);
            if (themeBean.getDownloadPath().equals(ThemeHelper.flashPath)) {
                mThemePreviewWindow.showPreview(true);
                mThemePreviewWindow.play(themeBean);
                mInCallActionView.setAutoRun(true);
                mCurrentView.setVisibility(View.VISIBLE);
            } else {
                mInCallActionView.setAutoRun(false);
                mThemePreviewWindow.showPreview(false);
                mThemePreviewWindow.stop();
                mCurrentView.setVisibility(View.GONE);
            }
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), CallThemeDetailActivity.class);
            intent.putExtra("position", position);
            intent.putParcelableArrayListExtra("data", (ArrayList<? extends Parcelable>) mList);
            getContext().startActivity(intent);
        }
    }
}
